package com.kehutong.payment.service;

import java.io.InputStream;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.orm.Pool;
import org.coraframework.util.Objects;
import com.kehutong.common.DoveClient;
import com.kehutong.common.urlconfig.FileUrlConfig;
import com.kehutong.payment.entity.MerchantSetting;
import com.kehutong.payment.entity.MerchantSettingCorp;
import com.kehutong.payment.vo.Merchant;

public class RouteService {

    @Inject
    private DoveClient doveClient;
    @Inject
    private FileUrlConfig fileUrlConfig;
    @Inject
    private MerchantSettingCorpService merchantCorpService;

    public Merchant route(String companyNo, String communityNo) throws Exception {
        if (Objects.nonEmpty(communityNo)) {
            String merchantSettingId = getCommunityMerchant(companyNo, communityNo);
            Merchant merchant = getMerchant(merchantSettingId);
            if (Objects.nonNull(merchant)) {
                return merchant;
            }
        }
        return getMerchant(companyNo);
    }

    public Merchant routeV2(String companyNo, String communityNo) throws Exception{
        MerchantSettingCorp merchantCorp = null;
        if (Objects.nonEmpty(communityNo)) {
            merchantCorp = merchantCorpService.getByCommunity(companyNo, communityNo);
        }

        if (Objects.isNull(merchantCorp)) {
            merchantCorp = merchantCorpService.getDefaultByCompanyNo(companyNo);
        }

        if (Objects.isNull(merchantCorp))
            throw new Exception("找不到默认的商户配置");

        if (merchantCorp.isMchModel()) {
            return getOrdinaryMerchant(merchantCorp);
        } else {
            return getProviderMerchant(merchantCorp);
        }

    }

    private Merchant getProviderMerchant(MerchantSettingCorp corpMerchant) throws Exception{
        if (Objects.isEmpty(corpMerchant.getMerchantSettingId()))
            throw new RuntimeException("unknown provider id config:" + corpMerchant.getId());

        MerchantSetting setting = Pool.get(MerchantSetting.class, corpMerchant.getMerchantSettingId());
        if (Objects.isNull(setting))
            throw new RuntimeException("unknown provider config:" + corpMerchant.getId());

        if (!setting.isProvider())
            throw new RuntimeException("not provider config:" + corpMerchant.getId());

        InputStream input = fileUrlConfig.download(setting.getMchFileId());
        if (Objects.isNull(input))
            throw new RuntimeException("unknown merchant no:" + corpMerchant.getId());


        return new Merchant(setting.getAppId(),
            setting.getMchId(),
            setting.getMchName(),
            setting.getMchSecret(),
            corpMerchant.getAppId(),
            corpMerchant.getMchId(),
            corpMerchant.getMchName(),
            input);
    }

    private Merchant getOrdinaryMerchant(MerchantSettingCorp corpMerchant) throws Exception {
        InputStream input = fileUrlConfig.download(corpMerchant.getMchFileId());
        if (Objects.isNull(input)) {
            throw new RuntimeException("unknown merchant no:" + corpMerchant.getId());
        }

        return new Merchant(corpMerchant.getAppId(),
            corpMerchant.getMchId(),
            corpMerchant.getMchName(),
            corpMerchant.getMchSecret(),
            corpMerchant.getAppId(),
            corpMerchant.getMchId(),
            corpMerchant.getMchName(),
            input);
    }

    private Merchant getMerchant(String id) throws Exception {
        MerchantSetting child = Pool.get(MerchantSetting.class, id);
        if (Objects.isNull(child)) {
            return null;
        }

        MerchantSetting parent = child;
        if (Objects.nonEmpty(child.getPid())) {
            parent = Pool.get(MerchantSetting.class, child.getPid());
        }

        InputStream input = fileUrlConfig.download(parent.getMchFileId());
        if (Objects.isNull(input)) {
            throw new RuntimeException("unknown merchant no:" + id);
        }

        return new Merchant(parent.getAppId(),
            parent.getMchId(),
            parent.getMchName(),
            parent.getMchSecret(),
            child.getAppId(),
            child.getMchId(),
            child.getMchName(),
            input);
    }

    private String getCommunityMerchant(String companyNo, String communityNo) throws Exception {
        JSONObject result = doveClient.post("/basic/community/merchant/service/get", http->{
            http.addHeader("companyNo", companyNo);
            http.addParam("communityNo", communityNo);
        });

        JSONObject data = result.getJSONObject("data");
        if (Objects.nonNull(data)) {
            return data.getString("merchantSettingId");
        }

        return null;
    }

}
